package com.aleaho.cftrack.utils;

import android.content.Context;

import com.aleaho.cftrack.R;
import com.aleaho.cftrack.activity.tree.BankViewHolder;
import com.aleaho.cftrack.activity.tree.HeaderViewHolder;
import com.aleaho.cftrack.activity.tree.IconTreeItemHolder;
import com.aleaho.cftrack.activity.tree.StaffViewHolder;
import com.aleaho.cftrack.entity.DepartmentAndUser;
import com.aleaho.cftrack.http.entity.DepartmentTreeEntity;
import com.unnamed.b.atv.model.TreeNode;

import java.util.List;

/**
 * Created by Administrator on 2017/6/26.
 */

public class TreeUtil {

    public static TreeNode entitiesToTreeNode(List<DepartmentTreeEntity> entities, TreeNode root, Context context) {

        TreeNode iRoot = root;
        for (DepartmentTreeEntity entity : entities) {
            iRoot.addChild(entityToTreeNode(entity, context));
        }
        return iRoot;
    }


    public static TreeNode entityToTreeNode(DepartmentTreeEntity entity, Context context) {
        DepartmentAndUser du = new DepartmentAndUser(0, entity.getDepartmentid(), entity.getDepartmentname(), entity.getDescription(), "");
        TreeNode node = null;
        if (entity.getParent() == null)
            node = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.icon_yinhang, du.mName, du)).setViewHolder(new HeaderViewHolder(context));
        else
            node = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.icon_yinhang, du.mName, du)).setViewHolder(new BankViewHolder(context));
        TreeNode fillNode = null;
        if (entity.getUser().size() > 0) {
            fillNode = userTreeNode(entity.getUser(), node, context);
            return fillNode;
        } else {
            return node;
        }
    }


    //    public static TreeNode entityToTreeNode(DepartmentTreeEntity entity) {
    //        DepartmentAndUser du = new DepartmentAndUser(0, entity.getDepartmentid(), entity.getDepartmentname(), entity.getDescription(), "");
    //        TreeNode node = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_home, du.mName, du));
    //        TreeNode fillNode = null;
    //        if (entity.getUser().size() > 0) {
    //            fillNode = userTreeNode(entity.getUser(), node);
    //        }
    //
    //        return fillNode;
    //    }


    public static TreeNode userTreeNode(List<DepartmentTreeEntity.UserBean> userBeens, TreeNode parentNode, Context context) {
        TreeNode pNode = parentNode;
        for (DepartmentTreeEntity.UserBean ub : userBeens) {
            DepartmentAndUser du = new DepartmentAndUser(1, String.valueOf(ub.getId()), ub.getName(), ub.getPermission().toString(), ub.getUsername());
            TreeNode node = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.icon_yuangong, du.mName, du)).setViewHolder(new StaffViewHolder(context));
            pNode.addChild(node);
        }

        return pNode;
    }


    public static DepartmentTreeEntity getTheRootDepartment(List<DepartmentTreeEntity> entities) {
        DepartmentTreeEntity rootEntity = null;

        for (DepartmentTreeEntity entity : entities) {
            if (null == entity.getParent())
                rootEntity = entity;
        }
        entities.remove(rootEntity);
        return rootEntity;
    }
}
